﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Script.Services;
using System.Web.Services;
using ZX.WebApp.CrmWorkFlowService;
using ZX.BLL.Extend;

namespace ZX.WebApp
{
    public partial class WorkflowCensor : System.Web.UI.Page
    {
        string operatorInsId = "";
        public string CtrlState;
        public string nowuser;
        public string Istransmit;
       
        ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient client = new CrmWorkFlowService.WorkFlowServiceClient();
        public string _Action = string.Empty;
        BaseBussnies basebussnies = new BaseBussnies();
        BaseUserControl baseuser = new BaseUserControl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Action"] != null )
                {
                    //delegate
                    _Action = Request.QueryString["Action"].ToString();
                    string lbWorkflowId =  Request.QueryString["WorkflowId"];
                    string lbWorktaskId =  Request.QueryString["WorktaskId"];
                    string lbWorkflowInsId =  Request.QueryString["WorkflowInsId"];
                    string lbWorktaskInsId =  Request.QueryString["WorktaskInsId"];
                    string lbAuditId =  Request.QueryString["AuditId"];
                    string lbOperatorInsId =  Request.QueryString["OperatorInsId"];
                    string lbUserName =  Request.QueryString["UserName"].ToString();
                    string tbxMessage = Request.QueryString["Message"].ToString();
                    string lbStatus = Request.QueryString["Status"].ToString();
                    string txtusername = Request.QueryString["txtusername"].ToString();
                    PostAjax(_Action, lbWorkflowId, lbWorktaskId, lbWorkflowInsId, lbWorktaskInsId, lbAuditId, lbOperatorInsId, lbUserName, tbxMessage, lbStatus, txtusername);
                }
                else
                {
                    ZX.EHR.Library.DatUser U = HttpContext.Current.Session["User"] as ZX.EHR.Library.DatUser;
                    if (Request["Status"].ToString() == "3")
                    {
                        //this.btnCustomId.Disabled = true;
                        //this.btnTransmitId.Disabled = true;
                        //this.tbxMessage.Enabled = false;
                    }
                    else
                    {
                        this.btnCustomId.Disabled = false;
                        this.btnTransmitId.Disabled = false;
                        this.tbxMessage.Enabled = true;
                    }
                    DataTable dt = client.GetWorkTaskInstanceView(Request["WorktaskInsId"].ToString());
                    DataTable dts = client.GetWorktaskInsStatus(Request["WorktaskInsId"].ToString());
                    if (dts.Rows.Count > 0)
                    {
                        if (dts.Rows[0]["OperType"].ToString() == "12")
                        {
                            //this.txtTransmitId.Visible = false;
                            //this.btnTransmitId.Visible = false;
                            //this.lbltext.Visible = false;
                        }
                    }
                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["OperType"].ToString() == "12")
                        {
                            this.txtTransmitId.Visible = false;
                            this.btnTransmitId.Visible = false;
                            this.lbltext.Visible = false;
                        }
                        if (dt.Rows[0]["TaskTypeId"].ToString() == "1" && dt.Rows[0]["OperStatus"].ToString() == "3" && dt.Rows[0]["Status"].ToString() == "2")
                        {
                            appMessge.Visible = false;
                            CtrlState = "1";
                        }
                    }
                    nowuser = U.LoginName.ToString();
                    InitData();
                }

            }
        }
        private void InitData()
        {
            if (Request["OperatorInsId"] != null)
            {
                ZX.EHR.Library.DatUser U = HttpContext.Current.Session["User"] as ZX.EHR.Library.DatUser;
                DataTable dt = client.GetAuditMsgTableByOper(Request["OperatorInsId"].ToString());
                if (dt != null && dt.Rows.Count > 0)
                {
                    string result = "";
                    lbAuditId.Text = dt.Rows[0]["AuditId"].ToString();
                    lbWorkflowId.Text = dt.Rows[0]["WorkflowId"].ToString();
                    lbWorkflowInsId.Text = dt.Rows[0]["WorkflowInsId"].ToString();
                    lbWorktaskId.Text = dt.Rows[0]["WorktaskId"].ToString();
                    lbWorktaskInsId.Text = dt.Rows[0]["WorktaskInsId"].ToString();
                    lbOperatorInsId.Text = dt.Rows[0]["OperatorInsId"].ToString();
                    //lbUserId.Text = dt.Rows[0]["AuditUserId"].ToString();
                    lbUserName.Text = dt.Rows[0]["AuditUserName"].ToString();
                    lblUserNames.Text = dt.Rows[0]["AuditUserName"].ToString();
                    lbArch.Text = dt.Rows[0]["AuditArch"].ToString();
                    lbDuty.Text = dt.Rows[0]["AuditDuty"].ToString();

                    result = dt.Rows[0]["AuditResult"].ToString();
                    //if (result == "同意")
                    //{
                    //    rbtOk.Checked = true;
                    //}
                    //else rbtNo.Checked = true;
                    lbAuditTime.Text = dt.Rows[0]["AuditTime"].ToString();
                    //tbxMessage.Text = dt.Rows[0]["Message"].ToString();
                    lbStatus.Text = WorkFlowConst.STATE_MOD;
                }
                else
                {
                    lbAuditId.Text = Guid.NewGuid().ToString();
                    lbWorkflowId.Text = Request["WorkflowId"];
                    lbWorkflowInsId.Text = Request["WorkflowInsId"];
                    lbWorktaskId.Text = Request["WorktaskId"];
                    lbWorktaskInsId.Text = Request["WorktaskInsId"];
                    lbOperatorInsId.Text = Request["OperatorInsId"];
                    //lbUserId.Text = U.LoginName.ToString();
                    lbUserName.Text = U.UName.ToString();
                    lblUserNames.Text = U.UName.ToString();
                    lbArch.Text = baseuser.ArchCaption;
                    lbDuty.Text = baseuser.DutyCaption;
                    lbStatus.Text = WorkFlowConst.STATE_ADD;
                    lbAuditTime.Text = DateTime.Now.ToShortDateString();
                }
                //DataTable dts = client.GetAuditMsgTableByFlow(Request["WorkflowInsId"]);
                DataView dts = client.GetWorkflowdetail(Request["WorkflowInsId"]).DefaultView;
                dts.Sort = "taskStartTime,OperDateTime Asc";
                rptCheckInfo.DataSource = dts;
                rptCheckInfo.DataBind();
            }
            ReturnControl(Request["OperatorInsId"].ToString());

        }

        public void GetWorkTaskTypeID(string WorkTaskInstanceId)
        {
            if (!string.IsNullOrEmpty(WorkTaskInstanceId))
            {
                string TaskTypeID = client.GetWorkTaskTypeID(WorkTaskInstanceId);
                    if (TaskTypeID == "1")
                    {
                        tdApproval.Visible = false;
                       
                    }
            }

        }
        public void ReturnControl(string operatorInsId)
        {
            if (!string.IsNullOrEmpty(operatorInsId))
            {
                DataTable dt = client.GetOperatorInstanceView(operatorInsId);
                if (dt.Rows.Count > 0)
                {
                    string status = dt.Rows[0]["Status"].ToString();
                    if (status == "4")
                    {
                        tdApproval.Visible = false;
                    }
                }
            }
            
        }
        /// <summary>
        /// 保存审批意见
        /// </summary>
        public void SaveUserControlApp(string lbWorkflowId, string lbWorktaskId, string lbWorkflowInsId, string lbWorktaskInsId, string lbAuditId, string lbOperatorInsId, string lbUserName, string tbxMessage, string lbStatus)
        {
            ZX.EHR.Library.DatUser U = HttpContext.Current.Session["User"] as ZX.EHR.Library.DatUser;
            CrmWorkFlowService.AuditMessage am = new CrmWorkFlowService.AuditMessage();
            am.AuditId = lbAuditId;
            am.WorkflowId = lbWorkflowId;
            am.WorkflowInsId = lbWorkflowInsId;
            am.WorktaskId = lbWorktaskId;
            am.WorktaskInsId = lbWorktaskInsId;
            am.OperatorInsId = lbOperatorInsId;
            //am.AuditUserId = lbUserId.Text;
            am.AuditUserName = lbUserName;
            DataTable dt = basebussnies.GetDeptandDuty(U.EmpID.ToString());
            if (dt.Rows.Count > 0)
            {
                am.AuditArch = dt.Rows[0]["DeptName"].ToString();
                am.AuditDuty = dt.Rows[0]["PostName"].ToString();
            }
            //if (rbtOk.Checked)
            //    am.AuditResult = "同意";
            //else
            //    if (rbtNo.Checked)
            //        am.AuditResult = "拒绝";
            am.Message = tbxMessage;
            DataTable dtTaskInsId = client.GetWorktaskInsStatus(lbWorktaskInsId);
            
            if (lbStatus == WorkFlowConst.STATE_ADD)
            {
                client.InsertAuditMessage(am);
            }
            else if (dtTaskInsId.Rows.Count >0)
            {
                client.InsertAuditMessage(am);
            }
            else
            {
                client.UpdateAuditMessage(am);
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            ////--------------------------todo zhuyun 20121012---------------------------------//
            if (!string.IsNullOrEmpty(txtTransmitId.Value))
            {
                string[] varuser = TransmitId.Value.Split(',');
                for (int i = 0; i < varuser.Length; i++)
                {
                    ZX.BLL.BUser buser = new ZX.BLL.BUser();
                    //System.Guid transmitid = new Guid(varuser[i].ToString());
                    //ZX.EHR.Library.DatUser duser = buser.GetDatUserByUserId(transmitid);
                    //client.DeleteByTaskInstance(lbWorktaskInsId.Text);
                    ZX.WebApp.CrmWorkFlowService.WorkTaskInstanceNextOperEntity entity = new ZX.WebApp.CrmWorkFlowService.WorkTaskInstanceNextOperEntity();
                    entity.NextOperId = Guid.NewGuid().ToString();
                    entity.UserId = varuser[i].ToString();
                    entity.UserName = "";
                    entity.WorkFlowId = lbWorkflowId.Text;
                    entity.WorkTaskId = lbWorktaskId.Text;
                    entity.WorkFlowInstanceId = lbWorkflowInsId.Text;
                    entity.WorkTaskInstanceId = lbWorktaskInsId.Text;
                    entity.Status = "0";
                    client.NextOperInsert(entity);
                }
            }

           
           // SaveUserControlApp();
                
            
        }

        public void PostAjax(string transmitId, string lbWorkflowId, string lbWorktaskId, string lbWorkflowInsId, string lbWorktaskInsId, string lbAuditId, string lbOperatorInsId, string lbUserName, string tbxMessage, string lbStatus,string txtusername)
        {
            if (transmitId !="")
            {
                string[] varuser = transmitId.Split(',');
                Session["transmitId"] = txtusername;
                for (int i = 0; i < varuser.Length; i++)
                {
                    ZX.BLL.BUser buser = new ZX.BLL.BUser();
                   
                    ZX.WebApp.CrmWorkFlowService.WorkTaskInstanceNextOperEntity entity = new
                    ZX.WebApp.CrmWorkFlowService.WorkTaskInstanceNextOperEntity();        
                    entity.NextOperId = Guid.NewGuid().ToString();
                    entity.UserId = varuser[i].ToString();
                    entity.UserName = "";
                    entity.WorkFlowId = lbWorkflowId;
                    entity.WorkTaskId = lbWorktaskId;
                    entity.WorkFlowInstanceId = lbWorkflowInsId;
                    entity.WorkTaskInstanceId = lbWorktaskInsId;
                    entity.Status = "0";
                    client.NextOperInsert(entity);
                }
            }
            SaveUserControlApp(lbWorkflowId, lbWorktaskId, lbWorkflowInsId, lbWorktaskInsId, lbAuditId, lbOperatorInsId, lbUserName, tbxMessage, lbStatus);
        }
    }
}